/*
 * @Author: 姚潘涛
 * @Date: 2024-04-08 15:18:03
 * @LastEditors: 姚潘涛
 * @LastEditTime: 2024-10-10 15:06:38
 * @FilePath:
 * /xict_ws/src/xcmg-intelligent-cockpit-three/src/xict_cpp/include/xict_cpp/core/stage/heartbeat_sender.h
 * @Description:
 *
 * Copyright (c) 2024 by pandaman, All Rights Reserved.
 */
#pragma once

#include "ecal/ecal.h"
#include "ecal/msg/protobuf/publisher.h"
#include "iostream"
#include "mutex"
#include "xict_common/utils/types.h"
#include "xict_cpp/bridge/udp_socket.h"
#include "xict_cpp/core/stage/base.h"
#include "xict_proto/excavator/js_state.pb.h"
#include "xict_proto/sensor/can.pb.h"

namespace xict_cpp {
    namespace core {
        namespace stage {
            struct HeartbeatSenderStageParams {
                /// @brief 是否开启心跳发送器，默认为false，不开启
                bool enable = false;
                /// @brief 心跳发送周期，单位s
                float frequency = 0.1;
                /// @brief 心跳发送器的IP地址
                std::string ip = "127.0.0.1";
                /// @brief 心跳发送器的端口
                int port = 5002;
                /// @brief 心跳指令
                std::string cmd_heartbeat = "HEARTBEAT";
                /// @brief 是否打印心跳日志
                bool is_log = false;
            };

            class HeartbeatSenderStage : public Stage {
            public:
                /// @brief 默认构造函数
                HeartbeatSenderStage() = default;
                /// @brief 默认析构函数
                ~HeartbeatSenderStage() override = default;

                /// @brief 初始化函数，初始化stage的状态和参数
                bool Init() override;
                /// @brief 带参数的初始化函数，初始化stage的状态和参数
                bool Init(const HeartbeatSenderStageParams& params);
                /// @brief 处理函数，处理stage的输入消息
                bool Process() override;
                /// @brief 返回类的名称
                std::string Name() const override {
                    return "HeartbeatSenderStage";
                }
                /// @brief 发送心跳数据
                void SendHeartBeat();

            protected:
                /// @brief 初始化函数，并返回结果
                bool Initialize() override;
                /// @brief 重置函数
                void Reset() override;

            private:
                std::shared_ptr<bridge::UdpSocket> heart_beat_sock_ptr_;
                boost::asio::io_service io_service_;
                /// @brief 互斥锁
                std::timed_mutex mtx_;
                /// @brief HeartBeatSenderStage的配置参数
                HeartbeatSenderStageParams params_;
                /// @brief 心跳发送器的状态
                bool is_running_ = false;
            };
        }   // namespace stage
    }       // namespace core
}   // namespace xict_cpp